//
//  WFStatusViewModel.swift
//  WangFangWeiBo
//
//  Created by 王方 on 16/7/13.
//  Copyright © 2016年 wf. All rights reserved.
//

import UIKit

class WFStatusViewModel: NSObject {
    
    //在这里面做显示的逻辑
    //转发数显示
    var retweeStr:String?
    //评论数显示内容
    var commentStr:String?
    //赞数显示内容
    var unlikeStr:String?
    //处理转发微博内容拼接"@xx:xxx"
    var retweetContent:String?
    //处理会员等级图片
    var mbrandImage:UIImage?
    //来源内容
    var sourceContent:String?
    //认证类型等级类型
    var verifiedTypeImage:UIImage?
    
    //发布时间
    var timeStr:String?
    
    //定义一个status作为这个viewmodel对status的持有
    var status:WFStatus?
    
    //  原创微博富文本
    var originalAttributedStr: NSAttributedString?
    
    //  转发微博富文本
    var retweetAttributedStr: NSAttributedString?
    
    init(status:WFStatus) {
        super.init()
        self.status = status
        retweeStr = handleCount(status.resposts_count, defaultTitle: "转发")
        commentStr = handleCount(status.comments_count, defaultTitle: "评论")
        unlikeStr = handleCount(status.attitudes_count, defaultTitle: "赞")
        
        //接下来处理转发微博的视图
        handleRetweetContent(status)
        //  处理会员等级图片
        handleMBrankImage(status.user?.mbrank ?? 0)
         //  处理来源字符串
        handleSourceContent(status.source ?? "")
        //  处理认证类型等级图片
        handleVerifiedTypeImage(status.user?.verified_type ?? 0)
        timeStr = NSDate.sinaDate(status.created_at ?? "").sinaDateString
        //处理原创微博内容的富文本
         originalAttributedStr = handleEmoticonContent(status.text!)
    }
    
    //通过微博内容查找表情描述转成富文本
    private func handleEmoticonContent(statusContent:String)->NSAttributedString{
        let result = NSMutableAttributedString(string: statusContent)
        
        var matchResultArray = [WFMatchResult]()
        (statusContent as NSString).enumerateStringsMatchedByRegex("\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]") { (matchCount, matchString, matchRange, _) -> Void in
            //  memory 是取到指针对应的内容
//            print(matchString.memory)
            
            if let chs = matchString.memory as? String {
//                let emoticon = WFEmoticonTool.sharedTools.selectEmoticonWithChs(chs)
//                print(emoticon)
                let matchResult  = WFMatchResult(matchString: chs, matchRange: matchRange.memory)
                matchResultArray.append(matchResult)
            }

        }
        
        for value in matchResultArray.reverse() {
            if let emoticon = WFEmoticonTool.sharedTools.selectEmoticonWithChs(value.matchString){
                let attributeStr = NSAttributedString.attributedStringWithEmoticon(emoticon, font: UIFont.systemFontOfSize(HomeFontSize))
                
                result.replaceCharactersInRange(value.matchRange, withAttributedString: attributeStr)
                
                
            }
            
        }
        
        return result
    }
    
    // MARK:  - 处理认证
    private func handleVerifiedTypeImage(veriType:Int){
        switch veriType {
        case 0 :
            verifiedTypeImage = UIImage(named:"avatar_vip")
        case 2,3,5 :
            verifiedTypeImage = UIImage(named: "avatar_enterprise_vip")
        case 220 :
            verifiedTypeImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedTypeImage = nil
        }
        
    }
    
    // MARK:  - 处理来源内容
    private func handleSourceContent(sourceStr:String){
        guard let rang1 = sourceStr.rangeOfString("\">"),let rang2 = sourceStr.rangeOfString("</") else {
            return
        }
        //代码执行到此,范围一定是获取到了的
        //开始光标
        let startIndex = rang1.endIndex
        //结束光标
        let endIndex = rang2.startIndex
        
        sourceContent = "来自 " + sourceStr.substringWithRange(startIndex..<endIndex)
    }
    
    // MARK:  - 处理会员等级
    private func handleMBrankImage(mbrank:Int){
        if mbrank >= 1 && mbrank <= 6 {
          let image =   UIImage(named: "common_icon_membership_level\(mbrank)")
            mbrandImage = image
        }
    }
    
    // MARK:  - 处理转发微博的内容
    private func handleRetweetContent(status:WFStatus){
        if status.retweeted_status?.text != nil {
            
            //获取转发用户的名字
            guard let name = status.retweeted_status?.user?.screen_name, let text = status.retweeted_status?.text else {
                return
            }
            retweetContent = "@\(name):\(text)"
            retweetAttributedStr = handleEmoticonContent(retweetContent!)
        }
    }
    
    //处理显示转发,评论,赞的数据样式
    /**
    *  1.判断count
    */
    private func handleCount(count:Int, defaultTitle:String)->String{
        
        if count > 0 {
            if count >= 10000 {
                let result = Float(count / 1000) / 10
                
                var resultStr = "\(result)万"
                
                if resultStr.containsString(".0") {
                 resultStr =  resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                }
                
                return resultStr
            } else {
                return "\(count)"
            }
            
        }else{
        return defaultTitle
        }
        
    }
    
}
